-
Notifications
You must be signed in to change notification settings - Fork 154
feat(logger): middy middleware #313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@@ -116,7 +134,7 @@ In both case, the printed log will look like this: | |||
"cold_start": true, | |||
"function_arn": "arn:aws:lambda:eu-central-1:123456789012:function:shopping-cart-api-lambda-prod-eu-central-1", | |||
"function_memory_size": 128, | |||
"function_request_id": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", | |||
"function_request_id": "c6af9ac6-7b61-11e6-9a41-93e812345678", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small change I did in this PR: removed all these weird "deadbeef" references
|
||
```typescript | ||
// Environment variables set for the Lambda | ||
process.env.LOG_LEVEL = 'INFO'; | ||
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world'; | ||
import { injectLambdaContext } from '../src/middleware/middy'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will update the relative paths of the examples in the readme file in another PR
@@ -7,7 +7,7 @@ process.env.POWERTOOLS_SERVICE_NAME = 'hello-world'; | |||
|
|||
import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json'; | |||
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world'; | |||
import { LambdaInterface } from './utils/lambda/LambdaInterface'; | |||
import { LambdaInterface } from '../types/LambdaInterface'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@flochaz the examples
folder referenced the types because of this one.
I move the LambdaInterface
inside the types
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
@@ -13,7 +13,8 @@ | |||
"resolveJsonModule": true, | |||
"pretty": true, | |||
"baseUrl": "src/", | |||
"rootDirs": [ "src/" ] | |||
"rootDirs": [ "src/" ], | |||
"esModuleInterop": true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related: middyjs/middy#203 (comment)
@@ -32,7 +32,7 @@ | |||
"Host": "1234567890.execute-api.eu-central-1.amazonaws.com", | |||
"Upgrade-Insecure-Requests": "1", | |||
"User-Agent": "Custom User Agent String", | |||
"Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", | |||
"Via": "1.1 08f32312345678a7af34d5feb414ce27.cloudfront.net (CloudFront)", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is again the "deadbeef" reference I replaced, in case you wonder
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job and thanks for the heavy lifting with the first middleware implementation!
.github/workflows/on-push.yml
Outdated
@@ -16,7 +16,7 @@ jobs: | |||
node-version: '14' | |||
- name: Install packages | |||
run: | | |||
npm ci | |||
npm install |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh nice catch
docs/core/logger.md
Outdated
import { Logger } from "@aws-lambda-powertools/logger"; | ||
import { injectLambdaContext } from '@aws-lambda-powertools/logger/middleware/middy'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we expose it at Top level, such as import { Middleware } from '@aws-lambda-powertools/logger';
?
logger.info("This is an INFO log with some context"); | ||
}; | ||
|
||
const handler = middy(lambdaHandler) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
middy not defined
@@ -91,6 +92,9 @@ const lambdaHandler: Handler = async () => { | |||
|
|||
}; | |||
|
|||
const handlerWithMiddleware = middy(lambdaHandler) | |||
.use(injectLambdaContext(logger)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure of the naming here, can't we get it from logger logger.Middleware. injectLambdaContext()
instead ? otherwise how do I know it's a logger middleware ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting observation. So here is my reasoning:
- With decorators being bound to a class in TypeScript, I thought it was intuitive to make it a method of the Logger class. That's not the case of a middleware.
- The expected Lambda middleware's DX is normally in the form of functional programming, see an example of typical middleware: https://github.com/middyjs/middy/blob/main/packages/http-cors/index.js
That being said, your observation still stands. How do I know it's a logger middleware? For now, through the explicit import from the Logger package, a mix IDE tooltips, the explicit type of the middleware's parameters (target: Logger | Logger []
).
In the future, to remove code duplication, I could see this middleware being moved in the commons
package and becoming "package agnostic": the middleware takes a logger instance, or a metrics instance, and so on, any instance that implements a common interface with the addContext
method.
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good !
@@ -7,7 +7,7 @@ process.env.POWERTOOLS_SERVICE_NAME = 'hello-world'; | |||
|
|||
import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json'; | |||
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world'; | |||
import { LambdaInterface } from './utils/lambda/LambdaInterface'; | |||
import { LambdaInterface } from '../types/LambdaInterface'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
@@ -91,6 +92,9 @@ const lambdaHandler: Handler = async () => { | |||
|
|||
}; | |||
|
|||
const handlerWithMiddleware = middy(lambdaHandler) | |||
.use(injectLambdaContext(logger)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good !
Description of your changes
Added:
How to verify this change
Related issues, RFCs
#294
PR status
Is this ready for review?: YES
Is it a breaking change?: NO
Checklist
Breaking change checklist
N/A
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.